<?php
class user_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    
    function get($_id){
        $where=array('_id'=>$_id);
        $query=$this->db->get_where("_type", $where); 
        return $query->result();
    }
    function checkLogin($_username="",$password=""){
        $where=array(
            'Email'=>$_username,
            "Delete"=>Null
            );
        $query=$this->db
                ->where($where)
                ->where("Authority IS NOT NULL")
                ->get("adp_oauth"); 
        $u=$query->result_array();
        if(isset($u[0]) && md5($password)==$u[0]["Password"]){
            $au=$u[0]["Authority"];
            $au= @preg_split("~;~", ";$au;", -1, PREG_SPLIT_NO_EMPTY);
            $oUser=$u[0];
            $oUser["Au"]=@$this->getauthotirys($au);
            $_SESSION["ADP"]["USER"]=$oUser;
            $_SESSION['KCFINDER']=null;
            if(checkAuthority("filemanage",false)){
                $_SESSION['KCFINDER']['disabled'] = false;
                $_SESSION['KCFINDER']['uploadURL'] = base_url("data");
                $_SESSION['KCFINDER']['uploadDir'] = '../../data';
                $_SESSION['KCFINDER']['access'] = array(
                    'files' => array(
                        'upload' => true,
                        'delete' => true,
                        'copy'   => true,
                        'move'   => true,
                        'rename' => true
                    ),
                    'dirs' => array(
                        'create' => true,
                        'delete' => true,
                        'rename' => true
                    )
                );
            }else{
                $_SESSION['KCFINDER']['uploadURL'] = base_url("f");
                $_SESSION['KCFINDER']['disabled'] = false;
                $_SESSION['KCFINDER']['access'] = array(
                    'files' => array(
                        'upload' => false,
                        'delete' => false,
                        'copy'   => false,
                        'move'   => false,
                        'rename' => false
                    ),
                    'dirs' => array(
                        'create' => false,
                        'delete' => false,
                        'rename' => false
                    )
                );
            }
            return true;
        }else{
            return false;
        }
        return false;
    }
    function getauthotirys($list=array()){
        $query=$this->db
        ->from("tbl_authority")
        ->where(array(
            "Delete"=>null
            ))
        ->where_in("Keyword",$list)
        ->get();
        return $query->result();
    }
    function gets(){
        $where=array('_delete'=>NULL);
        $query=$this->db
                ->where($where)
                ->get("_type"); 
        return $query->result();
    }
    
    function getByType($type=""){
        $where=array('_delete'=>NULL,"_type"=>$type);
        $query=$this->db
                ->where($where)
                ->get("_type"); 
        return $query->result();
    }
    function insert($params){
        $this->db->set('_insert', 'NOW()', FALSE);
        $this->db->insert("_type", $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function delete($_id){
        $this->db->set('_delete', 'NOW()', FALSE);
        $where=array('_id'=>$_id);
        $this->db->where($where);
        $this->db->update("_type"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return $count;
        return false;
    }
    function retore($_id){
        $this->db->set('_delete', 'NULL', FALSE);
        $where=array('_id'=>$_id);
        $this->db->where($where);
        $this->db->update("_type"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return $count;
        return false;
    }
    function update($_id,$params){
        $this->db->set('_update', 'NOW()', FALSE);
        $this->db->where('_id', $_id);
        $this->db->update("_type", $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return $count;
        return false;
    }
}
?>
